package com.xcw.bos.web.action.base;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.xcw.bos.page.PageBean;
import com.xcw.bos.service.IDecidedZoneService;
import com.xcw.bos.service.IFunctionService;
import com.xcw.bos.service.INoticebillService;
import com.xcw.bos.service.IRegionService;
import com.xcw.bos.service.IRoleService;
import com.xcw.bos.service.IStaffService;
import com.xcw.bos.service.ISubareaService;
import com.xcw.bos.service.IUserService;
import com.xcw.bos.service.IWorkordermanageService;

/**
 * 抽取表现层实现
 * @author xcw
 *
 */
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {

	//模型对象
	protected T model;
	public T getModel(){
		return model;
	}
	
	protected PageBean<T> pageBean = new PageBean<T>();
	DetachedCriteria detachedCriteria = null;
	
	//protected int page;//当前页码
	//protected int rows;//查询多少条数据
	
	public void setPage(int page) {
		pageBean.setPage(page);;
	}
	public void setRows(int rows) {
		pageBean.setPageSize(rows);
	}
	
	@Resource
	protected IUserService userService;
	@Resource
	protected IStaffService staffService;
	@Resource
	protected IRegionService regionService;
	@Resource
	protected ISubareaService subareaService;
	@Resource
	protected IDecidedZoneService decidedZoneService;
	@Resource
	protected INoticebillService noticebillService;
	@Resource
	protected IWorkordermanageService workordermanageService;
	@Resource
	protected IFunctionService functionService;
	@Resource
	protected IRoleService roleService;
	//在构造方法中获取实体类型，通过反射创建模型对象
	public BaseAction(){
		//获得父类(BaseAction)类型
		ParameterizedType genericSuperclass = null;
		Type superclass = this.getClass().getGenericSuperclass();
		if(superclass instanceof ParameterizedType){
			genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
		}else{
			genericSuperclass = (ParameterizedType) this.getClass().getSuperclass().getGenericSuperclass();
		}
		
		//ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
		//获得父类上的泛型数组
		Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
		Class<T> domainClass = (Class<T>) actualTypeArguments[0];
		detachedCriteria = DetachedCriteria.forClass(domainClass);
		pageBean.setDetachedCriteria(detachedCriteria);
		try {
			model = domainClass.newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
		
	public void writePageBean2Json(String[] excudes,PageBean pageBean){
			JsonConfig jc = new JsonConfig();
			jc.setExcludes(excudes);

			JSONObject jsonObject = JSONObject.fromObject(pageBean, jc);
			String json = jsonObject.toString();

			ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
			try {
				ServletActionContext.getResponse().getWriter().print(json);
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	public void writeList2Json(String[] excudes, List list){
		JsonConfig jc = new JsonConfig();
		jc.setExcludes(excudes);
		
		JSONArray jsonObject = JSONArray.fromObject(list, jc);
		String json = jsonObject.toString();
		
		ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
        try {
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void writeObject2Json(String[] excudes, Object object){
		JsonConfig jc = new JsonConfig();
		jc.setExcludes(excudes);
		
		JSONObject jsonObject = JSONObject.fromObject(object, jc);
		String json = jsonObject.toString();
		
		try {
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
